﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using S4Store.Service.Valid.ExhibitionKpiValid;
using S4Store.Domain.ExhibitionKpi;

namespace S4Store.Service
{
    public class ExhibitionKpiService : BaseService
    {
        public ExhibitionKpiMonthConfigSave ExhibitionKpiMonthConfigSave { get; set; }

        public IQueryable<ExhibitionMonthConfig> GetExhibitionMonthConfigQuery()
        {
            return this.GetQuery<ExhibitionMonthConfig>();
        }
        public IQueryable<ExhibitionWeekData> GetExhibitionWeekDataQuery()
        {
            return this.GetQuery<ExhibitionWeekData>();
        }


        public ExhibitionMonthConfig GetExhibitionMonthConfig(int year, int month, int storeid, bool onlycomfirmd = false)
        {
            if (onlycomfirmd)
            {
                ExhibitionMonthConfig config = this.GetExhibitionMonthConfigQuery().FirstOrDefault(t => t.Month.Year == year && t.Month.Month == month && t.StoreId == storeid && t.IsConfirmed);
                if (config == null)
                {
                    config = new ExhibitionMonthConfig();
                    config.Month = new DateTime(year, month, 1);
                    config.StoreId = storeid;
                }
                return config;
            }
            else
            {
                ExhibitionMonthConfig config = this.GetExhibitionMonthConfigQuery().FirstOrDefault(t => t.Month.Year == year && t.Month.Month == month && t.StoreId == storeid);
                if (config == null)
                {
                    config = new ExhibitionMonthConfig();
                    config.Month = new DateTime(year, month, 1);
                    config.StoreId = storeid;
                }
                return config;
            }
        }

        public void SubmitExhibitionMonthConfig(ExhibitionMonthConfig config)
        {
            this.Valid(config, ExhibitionKpiMonthConfigSave);
            if (config.Id == 0)
            {
                this.GetSet<ExhibitionMonthConfig>().Add(config);
            }

        }
        public IQueryable<ExhibitionWeekData> GetExhibitionWeekDatas(int year, int storeid, bool onlycomfirmd = false)
        {
            if (onlycomfirmd)
            {
                return GetExhibitionWeekDataQuery().Where(t => t.Month.Year == year&& t.StoreId == storeid && t.IsConfirmed);
            }
            else
            {
                return GetExhibitionWeekDataQuery().Where(t => t.Month.Year == year&& t.StoreId == storeid);
            }
        }

        public IQueryable<ExhibitionWeekData> GetExhibitionWeekDatas(int year, int month, int storeid, bool onlycomfirmd = false)
        {
            if (onlycomfirmd)
            {
                return GetExhibitionWeekDataQuery().Where(t => t.Month.Year == year && t.Month.Month == month && t.StoreId == storeid && t.IsConfirmed);
            }
            else
            {
                return GetExhibitionWeekDataQuery().Where(t => t.Month.Year == year && t.Month.Month == month && t.StoreId == storeid);
            }
        }


        public IQueryable<ExhibitionWeekData> GetExhibitionWeekDatas(int year, int month, int weekindex, int storeid)
        {
            return GetExhibitionWeekDataQuery().Where(t => t.Month.Year == year && t.Month.Month == month && t.WeekIndex == weekindex && t.StoreId == storeid);
        }

        public ExhibitionWeekData GetExhibitionWeekData(int year, int month,int weekindex, int storeid, bool onlycomfirmd = false)
        {
            if (onlycomfirmd)
            {
                return GetExhibitionWeekDataQuery().FirstOrDefault(t => t.Month.Year == year && t.Month.Month == month &&t.WeekIndex==weekindex&& t.StoreId == storeid && t.IsConfirmed);
            }
            else
            {
                return GetExhibitionWeekDataQuery().FirstOrDefault(t => t.Month.Year == year && t.Month.Month == month && t.WeekIndex == weekindex && t.StoreId == storeid);
            }
        }

        public void SubmitExhibitionWeekData(ExhibitionWeekData weekdata)
        {
            var datas = GetExhibitionWeekDataQuery().Where(t => t.Month.Year == weekdata.Month.Year && t.Month.Month == weekdata.Month.Month && t.WeekIndex == weekdata.WeekIndex && t.StoreId == weekdata.StoreId);
            foreach (var data in datas)
            {
                this.GetSet<ExhibitionWeekData>().Remove(data);
            }
            if (weekdata!=null)
            {
                this.GetSet<ExhibitionWeekData>().Add(weekdata);
            }
        }


        public ExhibitionKpiStandard SaveExhibitionKpiStandard(ExhibitionKpiStandard model)
        {
            var standard = this.GetQuery<ExhibitionKpiStandard>().FirstOrDefault(t => t.Id == model.Id);
            if (standard != null)
            {
                standard.GXJHSSLV = model.GXJHSSLV;
                standard.CTGGJKGXD = model.CTGGJKGXD;
                standard.CTGXZJDWLV = model.CTGXZJDWLV;
                standard.GXZJDWLV = model.GXZJDWLV;
                standard.CTGGZJZYLV = model.CTGGZJZYLV;
                standard.CTGGJKGXD = model.CTGGJKGXD;


                standard.DCKHJKCB = model.DCKHJKCB;
                standard.KHDYFXLV = model.KHDYFXLV;
                standard.ZTHDJKGXD = model.ZTHDJKGXD;
                standard.DTHDYYCGLV = model.DTHDYYCGLV;
                standard.ZTHDJHZXLV = model.ZTHDJHZXLV;
                standard.ZTHDCHCGLV = model.ZTHDCHCGLV;
                standard.ZTHDXGPGLV = model.ZTHDXGPGLV;
                standard.DTHDJHZXLV = model.DTHDJHZXLV;
                standard.DTHDCHCGLV = model.DTHDCHCGLV;
                standard.DTHDXGPGLV = model.DTHDXGPGLV;
                standard.DYQBSJZXLV = model.DYQBSJZXLV;
                standard.DTHDDCKHJKCB = model.DTHDDCKHJKCB;
                standard.ZTHDYYCGLV = model.ZTHDYYCGLV;
                standard.DTHDZBKECXHLV = model.DTHDZBKECXHLV;

                standard.ZTHDDCJKCB = model.ZTHDDCJKCB;
            }
            return standard;
        }

        public ExhibitionKpiStandard GetExhibitionKpiStandard(int year, int month, int storeid)
        {
            var model = this.GetQuery<ExhibitionKpiStandard>().FirstOrDefault(t => t.Month.Year == year && t.Month.Month == month && t.StoreId == storeid);
            if (model == null)
            {
                model = new ExhibitionKpiStandard();
                model.Month = new DateTime(year, month, 1);
                model.StoreId = storeid;
                var date = new DateTime(year, month, 1).AddMonths(-1);
                var lyear = date.Year;
                var lmonth = date.Month;
                var lastmodel = this.GetQuery<ExhibitionKpiStandard>().FirstOrDefault(t => t.Month.Year == lyear && t.Month.Month == lmonth && t.StoreId == storeid);
                if (lastmodel != null)
                {
                    model.CTGGJKGXD = lastmodel.CTGGJKGXD;
                    model.CTGXZJDWLV = lastmodel.CTGXZJDWLV;
                    model.GXZJDWLV = lastmodel.GXZJDWLV;
                    model.CTGGZJZYLV = lastmodel.CTGGZJZYLV;
                    model.CTGGJKGXD = lastmodel.CTGGJKGXD;

                    model.DCKHJKCB = lastmodel.DCKHJKCB;
                    model.KHDYFXLV = lastmodel.KHDYFXLV;
                    model.ZTHDJKGXD = lastmodel.ZTHDJKGXD;
                    model.DTHDYYCGLV = lastmodel.DTHDYYCGLV;
                    model.ZTHDJHZXLV = lastmodel.ZTHDJHZXLV;
                    model.ZTHDCHCGLV = lastmodel.ZTHDCHCGLV;
                    model.ZTHDXGPGLV = lastmodel.ZTHDXGPGLV;
                    model.DTHDJHZXLV = lastmodel.DTHDJHZXLV;
                    model.DTHDCHCGLV = lastmodel.DTHDCHCGLV;
                    model.DTHDXGPGLV = lastmodel.DTHDXGPGLV;
                    model.DYQBSJZXLV = lastmodel.DYQBSJZXLV;
                    model.DTHDDCKHJKCB = lastmodel.DTHDDCKHJKCB;
                    model.ZTHDYYCGLV = lastmodel.ZTHDYYCGLV;
                    model.DTHDZBKECXHLV = lastmodel.DTHDZBKECXHLV;
                }
                this.GetSet<ExhibitionKpiStandard>().Add(model);
            }
            return model;
        }

    }
}
